# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Mateusz Mikuła <mati865@gmail.com>
# Contributor: wirx6 <wirx654@gmail.com>

# We can switch to clang when it fully works with mingw-w64
#_compiler=clang

_realname=clang
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-clang-analyzer"
         "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
         "${MINGW_PACKAGE_PREFIX}-compiler-rt"
         "${MINGW_PACKAGE_PREFIX}-libc++"
         "${MINGW_PACKAGE_PREFIX}-libc++abi"
         "${MINGW_PACKAGE_PREFIX}-libunwind"
         "${MINGW_PACKAGE_PREFIX}-lld"
         "${MINGW_PACKAGE_PREFIX}-lldb"
         "${MINGW_PACKAGE_PREFIX}-llvm"
         "${MINGW_PACKAGE_PREFIX}-polly")
pkgver=6.0.0
pkgrel=2
pkgdesc="C language family frontend for LLVM (mingw-w64)"
arch=('any')
url="https://llvm.org/"
license=("custom:University of Illinois/NCSA Open Source License")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake>=3.4.3"
             "${MINGW_PACKAGE_PREFIX}-z3"
             $([[ "$_compiler" == "clang" ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-clang")
             "${MINGW_PACKAGE_PREFIX}-libffi"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python3-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "tar"
             "groff")
depends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('!debug' 'strip')
source=(https://releases.llvm.org/${pkgver}/llvm-${pkgver}.src.tar.xz{,.sig}
        https://releases.llvm.org/${pkgver}/cfe-${pkgver}.src.tar.xz{,.sig}
        https://releases.llvm.org/${pkgver}/compiler-rt-${pkgver}.src.tar.xz{,.sig}
        https://releases.llvm.org/${pkgver}/test-suite-${pkgver}.src.tar.xz{,.sig}
        https://releases.llvm.org/${pkgver}/libcxx-${pkgver}.src.tar.xz{,.sig}
        https://releases.llvm.org/${pkgver}/libcxxabi-${pkgver}.src.tar.xz{,.sig}
        https://releases.llvm.org/${pkgver}/clang-tools-extra-${pkgver}.src.tar.xz{,.sig}
        https://releases.llvm.org/${pkgver}/lld-${pkgver}.src.tar.xz{,.sig}
        https://releases.llvm.org/${pkgver}/lldb-${pkgver}.src.tar.xz{,.sig}
        https://releases.llvm.org/${pkgver}/libunwind-${pkgver}.src.tar.xz{,.sig}
        https://releases.llvm.org/${pkgver}/polly-${pkgver}.src.tar.xz{,.sig}
        "0001-mingw-w64-use-MSVC-style-ByteAlignment.patch"
        "0002-Fix-GetHostTriple-for-mingw-w64-in-msys.patch"
        "0003-Globally-provide-DESTDIR-for-all-platforms.patch"
        "0004-llvm-config-look-for-unversioned-shared-lib-on-win32.patch"
        "0005-add-pthread-as-system-lib-for-mingw.patch"
        "0006-fix-bugpoint-multiple-definition.patch"
        "0006-add-coff-exported-flag.patch"
        "0007-getLoadAddressWithOffset-assertion.patch"
        "0008-unmangled-getSymbolAddressInProcess.patch"
        "0009-empty-target-prefix-only-msvc.patch"
        "0010-mbig-obj-for-all.patch"
        "0011-different-name-for-static-libraries.patch"
        "0101-Allow-build-static-clang-library-for-mingw.patch"
        "0102-fix-libclang-name-for-mingw.patch"
        "0103-Set-the-x86-arch-name-to-i686-for-mingw-w64.patch"
        "0104-link-pthread-with-mingw.patch"
        "0105-use-emulated-tls-with-mingw.patch"
        "0106-MinGW-use-flto-visibility-public-std-CC1-arg-to-get-.patch"
        "0107-z3-fixes.patch"
        "0201-mingw-w64-__udivdi3-mangle-hack.patch"
        "0401-mingw-w64-hack-and-slash-fixes-for-libc.patch"
        "0402-D40181.patch"
        "0501-fixes-to-get-lldb-building.patch"
        "0502-hack-to-use-64-bit-time-for-mingw.patch"
        "0503-lldb-find-mingw-python.patch"
        "0504-fix-lldb-library-name-mingw.patch"
        "0505-use-gnu-printf.patch"
        "0506-mingw-python-posix-like.patch"
        "0507-declare-exports-for-all-windows-platforms.patch"
        "0801-Don-t-build-LLVMPolly-on-WIN32.patch")
# Some patch notes :)
#0001-0099 -> llvm
#0101-0199 -> clang
#0201-0299 -> rt
#0301-0399 -> lld
#0401-0499 -> libc++
#0501-0599 -> lldb
#0601-0699 -> libunwind
#0701-0799 -> libc++abi
#0801-0899 -> polly
sha256sums=('1ff53c915b4e761ef400b803f07261ade637b0c269d99569f18040f3dcee4408'
            'SKIP'
            'e07d6dd8d9ef196cfc8e8bb131cbd6a2ed0b1caf1715f9d05b0f0eeaddb6df32'
            'SKIP'
            'd0cc1342cf57e9a8d52f5498da47a3b28d24ac0d39cbc92308781b3ee0cea79a'
            'SKIP'
            '74e0055efa27b2143415148ee93b817155e6333337d9cadd4cc5d468ad3c0edf'
            'SKIP'
            '70931a87bde9d358af6cb7869e7535ec6b015f7e6df64def6d2ecdd954040dd9'
            'SKIP'
            '91c6d9c5426306ce28d0627d6a4448e7d164d6a3f64b01cb1d196003b16d641b'
            'SKIP'
            '053b424a4cd34c9335d8918734dd802a8da612d13a26bbb88fcdf524b2d989d2'
            'SKIP'
            '6b8c4a833cf30230c0213d78dbac01af21387b298225de90ab56032ca79c0e0b'
            'SKIP'
            '46f54c1d7adcd047d87c0179f7b6fa751614f339f4f87e60abceaa45f414d454'
            'SKIP'
            '256c4ed971191bde42208386c8d39e5143fa4afd098e03bd2c140c878c63f1d6'
            'SKIP'
            '47e493a799dca35bc68ca2ceaeed27c5ca09b12241f87f7220b5f5882194f59c'
            'SKIP'
            'd5b11097084f8a03dd3002c2adb27b4fec99cf290404049f1a4e1185274bde67'
            '5081a1a9d8074b275f3087d4d80a13ce5d1c20bc1962819953790e54275930c8'
            '46e10ce71525836d67466af954542c7133b646d7cc81dc30388f42dc27c7a6d6'
            'ca317e0d14dd52f115345c5e27f825d109654a43d5fc812ecdc883e0437677a1'
            '7f0c64cd87b61e894be632f180ae5291e1aa9f1d9d382608f659067eeeda7146'
            'f6e04b5e2a65e1eaa4ab6d14427cf17b1e79625a428430dd16a441578b31df55'
            '7140be792ec6806f3ed962839c85baecafe4d87df30c17d02cca476a37f6da8c'
            '1887ea21fcd591a50dd10559774e872ce1183177e672b028133d9b669e3cac32'
            '8fd8556f7b4f3adaabd5540f500bae1c3dc0daa7a23880c62860ed347c76b188'
            '33400d16d5f6671a8fd60345c3ae44b9777a7d600061957889d14305eb2ad709'
            '1c9efccd40a0e7834c3aa9d819aa25cfdd2cec389d1bd3e8a89bc9ff670a0129'
            '18e719f49af8704c66d7d5e0c0c94ab04a7206fe2a3d711eb652e0afb35ff52c'
            'f2b0403e6b6cafddf364a90a6bb1e57793fd98d6f80f8976a87574cd93eb1d1a'
            'b1ed2ccdb87bb4f024a5cd750e71de3706fd24ccfeb87835403dab84ef7a9019'
            'ab49b90d69a609f441fa58e835873f2b1a8a060c9ca6358fee7d99c1973da692'
            '53646dd01af2862473e9719c5223366486268891ccbff86413943a432a8342e9'
            '7b85221a5d8506abc622dc8f252866c93183ccd7e44d5e1bc082d6fcb244e7d3'
            '3d6045cf35d5523c68baf87bd490007d4ac8fe3eabcbc23d8c66e80ff1c234e7'
            'adbdc2edb31b4e3ff8be6f721f8052006420b819035f381f21475aab665a5c56'
            'bafaf7d06bec3ed7fba58e8a926e7b472b5e16442b6ee9dd8c7ee6c0cce9792a'
            'da44cca549bc7fb9e04f0f038a1ed68ba99cdfec834fc241ab13880bd0aa3b0f'
            '5e215239b99de4687b2ac133f5044bd9478e87b316df517dcc572ba67f9731d2'
            '0a2c657d8075616669bf076b7ec8d990f4c654ab0b4067d4e1bbc4ef0966e826'
            'a2bbb4d75d2d02b08a44a9f767b7f4ff11a546ae1ed79d06c3fc254218ac54e0'
            'bb78e162cccd926feadecd89e6352e12b40f3d3ccc6b444e4e39383cc44286c2'
            '769a8286ff528178256cc6001bb21562857ba9e9ab2214e5709b030d1b684ace'
            'b06014b98f96249386a75f3282c863a932efab5519a81dd68d28f8565ee91589'
            'dc758dfd564db316c717fe1ab201a432a47b2327ecf8835d28b1e2daad5962a6'
            '35b3fa7074594032ded30ed3fab4ef358b18fb0176c49e07bb33db9ed36057d4'
            'bc394e597f8939b6f6630bd88c990f951738aaadacded2f3be71c658e9608fe7')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '11E521D646982372EB577A1F8F0871F202119294') # Tom Stellard
noextract=(cfe-${pkgver}.src.tar.xz
           libcxx-${pkgver}.src.tar.xz
           lldb-${pkgver}.src.tar.xz
           test-suite-${pkgver}.src.tar.xz)

prepare() {
  plain "Extracting clang-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/cfe-${pkgver} ]] && rm -rf ${srcdir}/cfe-${pkgver}
  [[ -d ${srcdir}/cfe-${pkgver} ]] || tar -xJvf ${srcdir}/cfe-${pkgver}.src.tar.xz -C ${srcdir} || true

  plain "Extracting libcxx-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/libcxx-${pkgver} ]] && rm -rf ${srcdir}/libcxx-${pkgver}
  [[ -d ${srcdir}/libcxx-${pkgver} ]] || tar -xJvf ${srcdir}/libcxx-${pkgver}.src.tar.xz -C ${srcdir} || true

  plain "Extracting lldb-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/lldb-${pkgver} ]] && rm -rf ${srcdir}/lldb-${pkgver}
  [[ -d ${srcdir}/lldb-${pkgver} ]] || tar -xJvf ${srcdir}/lldb-${pkgver}.src.tar.xz -C ${srcdir} || true

  cd "${srcdir}/llvm-${pkgver}.src"
  patch -p1 -i "${srcdir}/0001-mingw-w64-use-MSVC-style-ByteAlignment.patch"
  patch -p1 -i "${srcdir}/0002-Fix-GetHostTriple-for-mingw-w64-in-msys.patch"
  patch -p1 -i "${srcdir}/0003-Globally-provide-DESTDIR-for-all-platforms.patch"
  patch -p1 -i "${srcdir}/0004-llvm-config-look-for-unversioned-shared-lib-on-win32.patch"
  patch -p1 -i "${srcdir}/0005-add-pthread-as-system-lib-for-mingw.patch"
  patch -p1 -i "${srcdir}/0009-empty-target-prefix-only-msvc.patch"
  patch -p1 -i "${srcdir}/0010-mbig-obj-for-all.patch"
  #patch -p1 -i "${srcdir}/0011-different-name-for-static-libraries.patch"

  # https://bugs.llvm.org/show_bug.cgi?id=25493
  patch -p1 -i "${srcdir}/0006-add-coff-exported-flag.patch"
  # https://bugs.llvm.org/show_bug.cgi?id=30584
  patch -p1 -i "${srcdir}/0007-getLoadAddressWithOffset-assertion.patch"
  # https://bugs.llvm.org/show_bug.cgi?id=28699
  patch -p1 -i "${srcdir}/0008-unmangled-getSymbolAddressInProcess.patch"

  #patch -p1 -i "${srcdir}/0006-fix-bugpoint-multiple-definition.patch"

  cd "${srcdir}/cfe-${pkgver}.src"
  patch -p1 -i "${srcdir}/0101-Allow-build-static-clang-library-for-mingw.patch"
  patch -p1 -i "${srcdir}/0102-fix-libclang-name-for-mingw.patch"
  patch -p1 -i "${srcdir}/0103-Set-the-x86-arch-name-to-i686-for-mingw-w64.patch"
  patch -p1 -i "${srcdir}/0104-link-pthread-with-mingw.patch"
  patch -p1 -i "${srcdir}/0105-use-emulated-tls-with-mingw.patch"
  patch -p1 -i "${srcdir}/0106-MinGW-use-flto-visibility-public-std-CC1-arg-to-get-.patch"
  patch -p1 -i "${srcdir}/0107-z3-fixes.patch"

  cd "${srcdir}/compiler-rt-${pkgver}.src"
  patch -p1 -i "${srcdir}/0201-mingw-w64-__udivdi3-mangle-hack.patch"

  #cd "${srcdir}/lld-${pkgver}.src"

  cd "${srcdir}/libcxx-${pkgver}.src"
  patch -p1 -i "${srcdir}/0401-mingw-w64-hack-and-slash-fixes-for-libc.patch"
  patch -p1 -R -i "${srcdir}/0402-D40181.patch"

  cd "${srcdir}/lldb-${pkgver}.src"
  patch -p1 -i "${srcdir}/0501-fixes-to-get-lldb-building.patch"
  patch -p1 -i "${srcdir}/0503-lldb-find-mingw-python.patch"
  patch -p1 -i "${srcdir}/0504-fix-lldb-library-name-mingw.patch"
  patch -p1 -i "${srcdir}/0505-use-gnu-printf.patch"
  patch -p1 -i "${srcdir}/0506-mingw-python-posix-like.patch"
  patch -p1 -i "${srcdir}/0507-declare-exports-for-all-windows-platforms.patch"
  
  # There was big refactor/cleanup and it doesn't apply any more
  # https://github.com/llvm-mirror/lldb/commit/456b31d616f3c71be31315ae26f6c8e174f4fd51#diff-2c5e3e9a387d499896aa082d15141bfb
  # However 32 bit lldb is still crashing without it; temporary leaving patch for the reference until it's solved 
  #patch -p1 -i "${srcdir}/0502-hack-to-use-64-bit-time-for-mingw.patch"

  #cd "${srcdir}/libunwind-${pkgver}.src"

  #cd "${srcdir}/libcxxabi-${pkgver}.src"

  cd "${srcdir}/polly-${pkgver}.src"
  # With LLVM_LINK_LLVM_DYLIB=ON it's build before LLVM and fails with undefined references to LLVM functions
  patch -p1 -i "${srcdir}/0801-Don-t-build-LLVMPolly-on-WIN32.patch"

  # At the present, clang must reside inside the LLVM source code tree to build
  # See https://bugs.llvm.org/show_bug.cgi?id=4840

  cd "${srcdir}"/llvm-${pkgver}.src
  rm -rf tools/clang tools/clang/tools/extra tools/lld tools/lldb tools/polly projects/compiler-rt projects/libcxx \
         projects/libcxxabi projects/libunwind projects/test-suite | true
  mv "${srcdir}/cfe-${pkgver}.src"               tools/clang
  mv "${srcdir}/clang-tools-extra-${pkgver}.src" tools/clang/tools/extra
  mv "${srcdir}/lld-${pkgver}.src"               tools/lld
  mv "${srcdir}/lldb-${pkgver}.src"              tools/lldb
  mv "${srcdir}/polly-${pkgver}.src"             tools/polly
  mv "${srcdir}/compiler-rt-${pkgver}.src"       projects/compiler-rt
  mv "${srcdir}/libcxxabi-${pkgver}.src"         projects/libcxxabi
  mv "${srcdir}/libcxx-${pkgver}.src"            projects/libcxx
  mv "${srcdir}/libunwind-${pkgver}.src"         projects/libunwind
  #mv "${srcdir}/testsuite-${pkgver}.src"         projects/test-suite
}

build() {
  cd "${srcdir}"

  [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  mkdir build-${CARCH} && cd build-${CARCH}

  local -a extra_config

  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
    VERBOSE="VERBOSE=1"
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  # Include location of libffi headers in CPPFLAGS
  FFI_INCLUDE_DIR="$(pkg-config --cflags libffi)"
  FFI_INCLUDE_DIR=$(echo $FFI_INCLUDE_DIR | sed 's|-I||g')

  # "Ninja" cant install each component seperately
  # https://github.com/martine/ninja/issues/932

  if [ "${_compiler}" == "clang" ]; then
    #export CC='clang -stdlib=libc++'
    #export CXX='clang++ -stdlib=libc++'
    export CC='clang'
    export CXX='clang++'
  fi

  CFLAGS+=" -D_WIN32_WINNT=0x0600"
  CXXFLAGS+=" -D_WIN32_WINNT=0x0600"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DCMAKE_MAKE_PROGRAM="/usr/bin/make.exe" \
    -DFFI_INCLUDE_DIR="${FFI_INCLUDE_DIR}" \
    -DCMAKE_C_FLAGS="${CFLAGS}" \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS} -D_GNU_SOURCE" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCLANG_ANALYZER_BUILD_Z3=ON \
    -DLIBCLANG_BUILD_STATIC=ON \
    -DLIBCXX_ENABLE_SHARED=OFF \
    -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF \
    -DLIBCXXABI_ENABLE_SHARED=OFF \
    -DLIBUNWIND_ENABLE_SHARED=OFF \
    -DLLVM_TARGETS_TO_BUILD="ARM;NVPTX;X86" \
    -DLLVM_BUILD_STATIC=OFF \
    -DLLVM_BUILD_LLVM_DYLIB=ON \
    -DLLVM_ENABLE_ASSERTIONS=OFF \
    -DLLVM_ENABLE_FFI=ON \
    -DLLVM_ENABLE_THREADS=ON \
    -DLLVM_ENABLE_SPHINX=ON \
    -DLLVM_LINK_LLVM_DYLIB=OFF \
    -DLLVM_POLLY_LINK_INTO_TOOLS=OFF \
    -DLLDB_RELOCATABLE_PYTHON=ON \
    -DLLDB_USE_SYSTEM_SIX=ON \
    -DLLDB_DISABLE_PYTHON=ON \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python2.exe \
    "${extra_config[@]}" \
    ../llvm-${pkgver}.src

  make ${VERBOSE}

  # Disable automatic installation of components that go into subpackages
  # -i.orig to check what has been removed in-case it starts dropping more than it should
  #
  sed -i.orig '/\(clang\|lld\|lldb\|polly\)\/cmake_install.cmake/d' tools/cmake_install.cmake
  sed -i.orig '/\(extra\|scan-build\|scan-view\)\/cmake_install.cmake/d' tools/clang/tools/cmake_install.cmake
# sed -i.orig '/\(compiler-rt\|libcxxabi\|libcxx\)\/cmake_install.cmake/d' projects/cmake_install.cmake
  sed -i.orig '/\(compiler-rt\|libcxxabi\|libcxx\|libunwind\)\/cmake_install.cmake/d' projects/cmake_install.cmake
}

#check() {
#  cd "${srcdir}"/build-${CARCH}
#  # Remove || true once testcase doesn't cause failures.
#  make check || true
#  make check-clang || true
#  make check-polly || true
#}

package_clang() {
  pkgdesc="C language family frontend for LLVM (mingw-w64)"
  url="https://clang.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-gcc"
           "${MINGW_PACKAGE_PREFIX}-z3")

  cd "${srcdir}/llvm-${pkgver}.src"
  make -C ../build-${CARCH}/tools/clang DESTDIR="${pkgdir}" install

  # Install static libraries
  install -Dm644 ../build-${CARCH}/lib/libclang.a ${pkgdir}${MINGW_PREFIX}/lib/libclang.a
  install -Dm644 ../build-${CARCH}/lib/libclang.a ${pkgdir}${MINGW_PREFIX}/lib/libclang_static.a
}

package_clang-analyzer() {
  pkgdesc="A source code analysis framework (mingw-w64)"
  url="https://clang-analyzer.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-clang=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-python2")

  cd "${srcdir}/llvm-${pkgver}.src"
  make -C ../build-${CARCH}/tools/clang/tools/scan-build -j1 DESTDIR="${pkgdir}" install
  make -C ../build-${CARCH}/tools/clang/tools/scan-view -j1 DESTDIR="${pkgdir}" install

  # Use Python 2
  sed -i \
    -e 's|env python$|&2|' \
    -e 's|/usr/bin/python$|&2|' \
    "${pkgdir}"${MINGW_PREFIX}/bin/scan-view

  # Compile Python scripts
  python2 -m compileall "${pkgdir}"${MINGW_PREFIX}/bin/clang-analyzer
  python2 -O -m compileall "${pkgdir}"${MINGW_PREFIX}/lib/clang-analyzer
  install -Dm644 LICENSE.TXT "${pkgdir}"${MINGW_PREFIX}/share/licenses/clang-analyzer/LICENSE
}

package_clang-tools-extra() {
  pkgdesc="Extra tools built using Clang's tooling APIs (mingw-w64)"
  url="https://clang.llvm.org/"

  cd "${srcdir}/llvm-${pkgver}.src"
  make -C ../build-${CARCH}/tools/clang/tools/extra -j1 DESTDIR="${pkgdir}" install
}

package_compiler-rt() {
  pkgdesc="Runtime libraries for Clang and LLVM (mingw-w64)"
  url="https://compiler-rt.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}-${pkgrel}")

  cd "${srcdir}"/llvm-${pkgver}.src
  make -C ../build-${CARCH}/projects/compiler-rt DESTDIR="${pkgdir}" install
}

package_libcxx() {
  pkgdesc="C++ Standard Library (mingw-w64)"
  url="https://libcxx.llvm.org/"

  cd "${srcdir}/llvm-${pkgver}.src"
  make -C ../build-${CARCH}/projects/libcxx -j1 DESTDIR="${pkgdir}" install
}

package_libcxxabi() {
  pkgdesc="C++ Standard Library Support (mingw-w64)"
  url="https://libcxxabi.llvm.org/"
  # Don't depend on libunwind until it actually works as installing it breaks some packages
  #depends="${MINGW_PACKAGE_PREFIX}-libunwind"

  cd "${srcdir}/llvm-${pkgver}.src"
  make -C ../build-${CARCH}/projects/libcxxabi -j1 DESTDIR="${pkgdir}" install
}

package_libunwind() {
  pkgdesc='A new implementation of a stack unwinder for C++ exceptions (mingw-w64)'
  url='https://llvm.org/'

  cd "${srcdir}/llvm-${pkgver}.src"
  make -C ../build-${CARCH}/projects/libunwind -j1 DESTDIR="${pkgdir}" install
}

package_lld() {
  pkgdesc="Linker tools for LLVM (mingw-w64)"
  url="https://lld.llvm.org/"

  cd "${srcdir}/llvm-${pkgver}.src"
  make -C ../build-${CARCH}/tools/lld -j1 DESTDIR="${pkgdir}" install
}

package_lldb() {
  pkgdesc="Next generation, high-performance debugger (mingw-w64)"
  url="https://lldb.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
           "${MINGW_PACKAGE_PREFIX}-llvm"
           "${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-readline"
           "${MINGW_PACKAGE_PREFIX}-swig")

  cd "${srcdir}/llvm-${pkgver}.src"

  make -C ../build-${CARCH}/tools/lldb DESTDIR="${pkgdir}" install

  # Compile Python scripts
  python2 -m compileall "${pkgdir}${MINGW_PREFIX}/lib/python2.7/site-packages/lldb"
  python2 -O -m compileall "${pkgdir}${MINGW_PREFIX}/lib/python2.7/site-packages/lldb"

  install -Dm644 tools/lldb/LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/lldb/LICENSE"
}

package_llvm() {
  pkgdesc="Low Level Virtual Machine (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libffi"
           "${MINGW_PACKAGE_PREFIX}-gcc-libs") # "compiler-rt"

  cd "${srcdir}"/llvm-${pkgver}.src

  make -C ../build-${CARCH} DESTDIR="${pkgdir}" install

  # TODO: why CMake haven't included it?
  #cp ../build-${CARCH}/lib/libLLVM.dll.a "${pkgdir}/${MINGW_PREFIX}/lib/"

  install -Dm644 LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/llvm/LICENSE"

  # Install CMake stuff
  install -d "${pkgdir}"${MINGW_PREFIX}/share/llvm/cmake/{modules,platforms}
  install -Dm644 "${srcdir}"/llvm-${pkgver}.src/cmake/modules/*.cmake "${pkgdir}"${MINGW_PREFIX}/share/llvm/cmake/modules/
  install -Dm644 "${srcdir}"/llvm-${pkgver}.src/cmake/platforms/*.cmake "${pkgdir}"${MINGW_PREFIX}/share/llvm/cmake/platforms/

  # FileCheck is needed to build rust.
  install -Dm755 "${srcdir}"/build-${CARCH}/bin/FileCheck.exe "${pkgdir}${MINGW_PREFIX}/bin/FileCheck.exe"
  # fox cmake files.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${pkgdir}/${MINGW_PREFIX}/lib/cmake/llvm/LLVMExports.cmake
}

package_polly() {
  pkgdesc="Polly - Polyhedral optimizations for LLVM (mingw-w64)"
  url="https://polly.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}-${pkgrel}")

  cd "${srcdir}"/llvm-${pkgver}.src
  make -C ../build-${CARCH}/tools/polly DESTDIR="${pkgdir}" install
  # fox cmake files.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -e "s|${PREFIX_WIN}/bin/|${MINGW_PREFIX}/|g" -i "${pkgdir}/${MINGW_PREFIX}/lib/cmake/polly/PollyConfig.cmake"
}

# Wrappers
package_mingw-w64-i686-clang(){
  package_clang
}

package_mingw-w64-i686-clang-analyzer(){
  package_clang-analyzer
}

package_mingw-w64-i686-clang-tools-extra(){
  package_clang-tools-extra
}

package_mingw-w64-i686-compiler-rt(){
  package_compiler-rt
}

package_mingw-w64-i686-libc++abi(){
  package_libcxxabi
}

package_mingw-w64-i686-libc++(){
  package_libcxx
}

package_mingw-w64-i686-lld(){
  package_lld
}

package_mingw-w64-i686-lldb(){
  package_lldb
}

package_mingw-w64-i686-libunwind(){
  package_libunwind
}

package_mingw-w64-i686-llvm(){
  package_llvm
}

package_mingw-w64-i686-polly(){
  package_polly
}

package_mingw-w64-x86_64-clang(){
  package_clang
}

package_mingw-w64-x86_64-clang-analyzer(){
  package_clang-analyzer
}

package_mingw-w64-x86_64-clang-tools-extra(){
  package_clang-tools-extra
}

package_mingw-w64-x86_64-compiler-rt(){
  package_compiler-rt
}

package_mingw-w64-x86_64-libc++abi(){
  package_libcxxabi
}

package_mingw-w64-x86_64-libc++(){
  package_libcxx
}

package_mingw-w64-x86_64-lld(){
  package_lld
}

package_mingw-w64-x86_64-lldb(){
  package_lldb
}

package_mingw-w64-x86_64-libunwind(){
  package_libunwind
}

package_mingw-w64-x86_64-llvm(){
  package_llvm
}

package_mingw-w64-x86_64-polly(){
  package_polly
}
